<?php
/**
 * 银联支付控制类
 */
namespace Home\Controller;
use Think\Controller;
class UpmpPayController extends Controller{
    //同步处理
	public function call_back(){
		//去掉TP的GET参数，否则可能影响支付宝计算验证结果
		unset($_GET['_URL_'],$_POST['_URL_'],$_REQUEST['_URL_']);
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');
        import("AppUpmpService",'Public/Class/Pay/UPMP');
		$cert_path = D('File')->get_file_info($pay_config['upmp_web_pfx_file'], 'filepath');
		$upmp = new \AppUpmpService('web', $pay_config['upmp_web_merchant_id'], $cert_path, $pay_config['upmp_web_cert_pwd']);
		$this->log_path = $upmp->logs_path.'upmp_web_'.date('Y_m_d').'.log';
		//计算得出通知验证结果
		$notify_info = $_REQUEST;
		$this->logs('[异步]银联参数内容：' . print_r($notify_info, true));

		$verify_info = $upmp->verify($notify_info);

		if ($verify_info) {
			$this->logs('验证成功');
			$this->logs('验证内容：' . print_r($verify_info, true));
			//商户订单号
			$order_sn     = $notify_info['orderId'];
			//支付宝交易号
			$trade_no     = $notify_info['queryId'];
			//支付金额
			$total_fee    = $notify_info['txnAmt'] / 100;
			//买方支付账号
			$buyer_email  = $notify_info['accNo'] ? $notify_info['accNo'] : $notify_info['traceNo'];
			//卖方账号
			$seller_email = $notify_info['merId'];

			$where['order_sn'] = $order_sn;
			$order = D('Order')->where($where)->find();
			//买方ID
			$buyer_id    = $order['user_id'];

			if(!$order){
				$this->logs('此订单号不存在！');
				exit("fail");
			}
			if($order['status'] != 'wait_pay'){
				$this->logs('此订单状态不能进行支付，订单状态：' . $order['status'] . '可支付状态：wait_pay');
				redirect(URL('UserOrder', array('order_sn' => $order_sn)));
			}

			$this->logs('提交支付成功订单......');
			//提交订单支付
			D('Order')->order_pay('upmp',
								  $order_sn,
								  $trade_no,
								  $total_fee,
								  $buyer_email,
								  $buyer_id,
								  $seller_email);

			$this->logs('[同步]网站平台==>支付成功！');

			redirect(URL('UserOrder', array('order_sn' => $order_sn)));
		}
		else {
			$this->logs('[同步]支付宝==>验证失败！');
		    $this->error('支付失败！');
		}
	}
	//异步处理
	public function notify_url(){
		//去掉TP的GET参数，否则可能影响支付宝计算验证结果
		unset($_GET['_URL_'],$_POST['_URL_'],$_REQUEST['_URL_']);
        // 支付配置
		$pay_config = D('Config')->get('', 'pay');
        import("AppUpmpService",'Public/Class/Pay/UPMP');
		$cert_path = D('File')->get_file_info($pay_config['upmp_web_pfx_file'], 'filepath');
		$upmp = new \AppUpmpService('web', $pay_config['upmp_web_merchant_id'], $cert_path, $pay_config['upmp_web_cert_pwd']);
		$this->log_path = $upmp->logs_path.'upmp_web_'.date('Y_m_d').'.log';
		//计算得出通知验证结果
		$notify_info = $_REQUEST; // 官方文档所说的post是接收不到参数的，实际上要用get才行
		$this->logs('[异步]银联参数内容：' . print_r($notify_info,true));

		$verify_info = $upmp->verify($notify_info);

		if($verify_info) {//验证成功
			$this->logs('验证成功');
			$this->logs('验证内容：' . print_r($verify_info,true));
			//商户订单号
			$order_sn     = $notify_info['orderId'];
			//支付宝交易号
			$trade_no     = $notify_info['queryId'];
			//支付金额
			$total_fee    = $notify_info['txnAmt'] / 100;
			//买方支付账号
			$buyer_email  = $notify_info['accNo'] ? $notify_info['accNo'] : $notify_info['traceNo'];
			//卖方账号
			$seller_email = $notify_info['merId'];

			$where['order_sn'] = $order_sn;
			$order = D('Order')->where($where)->find();
			//买方ID
			$buyer_id = $order['user_id'];
			if(!$order){
				$this->logs('此订单号不存在！');
				exit("fail");
			}
			if($order['status'] != 'wait_pay'){
				$this->logs('此订单状态不能进行支付，订单状态：' . $order['status'] . '可支付状态：wait_pay');
				exit('fail');
			}

			$this->logs('提交支付成功订单......');
			//提交订单支付
			D('Order')->order_pay('upmp',
								  $order_sn,
								  $trade_no,
								  $total_fee,
								  $buyer_email,
								  $buyer_id,
								  $seller_email);

			$this->logs('[异步]网站平台==>支付成功！');

			exit('success');
		}
		//验证失败
		else {
			$this->logs('[异步]银联支付==>验证失败');
		    exit('fail');
		}
	}
	public function merchant(){
		$this->error('取消交易',U('Index/index'));
	}
	//写入日志
	protected function logs($content){
		\Think\Log::write($content, \Think\Log::INFO, '', $this->log_path);
	}
}
?>